﻿using System;
using System.Collections.Generic;
using NTTDATA.BEENChina.ResultCertification.Config;
using NTTDATA.BEENChina.ResultCertification.Split;

namespace NTTDATA.BEENChina.ResultCertification.Checker.Date
{
    [Serializable]
    [VerfityAttribute(Id = "VerfityRq0224", Name = "不存在日期检证", CategoryType = EnumCategoryType.日期, Model = true)]
    public class VerfityRq0224 : VerfityOneResult
    {
       /// <summary>
       /// 20130936 modify 包含? 的录入结果忽略检查
       /// </summary>
       /// <param name="result"></param>
       /// <param name="entryCommon"></param>
       /// <returns></returns>
        public override bool CheckResult(List<List<string>> result, string entryCommon)
        {
            bool bresult = false;

            for (int i = 0; i < result.Count && !bresult; i++) 
            {
                List<string> row = result[i];
                if( row == null)
                {
                    continue ;
                }
                for(int j=0;j<row.Count && !bresult;j++)
                {
                    string entryDateStr = row[j];

                    if (EmptyResultCount(GetSpecialResult( entryDateStr)) > 0)
                    {
                        continue;
                    }
                    //entryDateStr 可能包含问号 24/2/8？
                    //带?的排除掉
                    if (!entryDateStr.Contains("?"))
                    {
                        entryDateStr = CompleteDate(entryDateStr);
                        DateTime? entryDate = GetEntryDate(entryDateStr);
                        bresult = !(entryDate != null && entryDate.Value.Year - DateTime.Now.Year < 90);
                    }
                }
            }
            if (bresult)
            {
                System.Console.WriteLine("VerfityRQ0224");
            }

            return bresult;
        }

        private DateTime? GetEntryDate(string entryResult)
        {
            if (string.IsNullOrEmpty(entryResult))
            {
                return null;
            }

            entryResult = new Split.DateHelper(ChildImageObject.CategorySetting).ChangeDateStr(entryResult);
            
            var splitEntryResult = new Split.SplitEntryResult(ChildImageObject.CategorySetting.DataFormat, entryResult);
            DateTime? entryDate = splitEntryResult.GetDateTime();

            return entryDate;
        }

        private string GetSpecialResult(string entryResult)
        {
            if (string.IsNullOrEmpty(entryResult))
            {
                return null;
            }

            entryResult = new Split.DateHelper(ChildImageObject.CategorySetting).ChangeDateStr(entryResult);

            var splitEntryResult = new Split.SplitEntryResult(ChildImageObject.CategorySetting.DataFormat, entryResult);
  

            return  splitEntryResult.GetDate();
        }

      /*  private void ComplementDate(ref string date)
        {
            var dateHelper = new DateHelper(ChildImageObject.CategorySetting);

            //公历变换和历
            date = dateHelper.ChangeDateStr(date);

            //年月日
            date = dateHelper.CompleteDate(date); 
  
 
        }
*/
    }
}
